package com.jz.community.moduleshoppingguide.home.ui.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.view.ViewGroup;

import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.moduleshoppingguide.home.bean.CategoriesBean;
import com.jz.community.moduleshoppingguide.home.ui.fragment.HomeFindFragment;
import com.jz.community.moduleshoppingguide.home.ui.fragment.ShoppingGuidePageFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends FragmentStatePagerAdapter {


    private List<CategoriesBean> list;
    private FragmentManager fm;
    private HomeFindFragment homeFindFragment;
    private List<Fragment> itemFragmentList;

    public HomeFindFragment getHomeFindFragment() {
        return homeFindFragment;
    }

    public HomeAdapter(FragmentManager fm, List<CategoriesBean> list, List<Fragment> itemFragmentList) {
        super(fm);
        this.fm = fm;
        this.list = list;
        this.itemFragmentList = itemFragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return itemFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getName();
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    /* @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (position == 0) {
            //拿到缓存的fragment，如果没有缓存的，就新建一个，新建发生在fragment的第一次初始化时
            homeFindFragment = (HomeFindFragment) super.instantiateItem(container, position);
            String fragmentTag = homeFindFragment.getTag();
            if (homeFindFragment != getItem(position)) {
                //如果是新建的fragment，f 就和getItem(position)是同一个fragment，否则进入下面
                FragmentTransaction ft = fm.beginTransaction();
                //移除旧的fragment
                ft.remove(homeFindFragment);
                //换成新的fragment
                homeFindFragment = (HomeFindFragment) getItem(position);
                //添加新fragment时必须用前面获得的tag
                ft.add(container.getId(), homeFindFragment, fragmentTag);
                ft.attach(homeFindFragment);
                ft.commitAllowingStateLoss();
            }
            return homeFindFragment;
        } else {
            //拿到缓存的fragment，如果没有缓存的，就新建一个，新建发生在fragment的第一次初始化时
            ShoppingGuidePageFragment f = (ShoppingGuidePageFragment) super.instantiateItem(container, position);
            String fragmentTag = f.getTag();
            if (f != getItem(position)) {
                //如果是新建的fragment，f 就和getItem(position)是同一个fragment，否则进入下面
                FragmentTransaction ft = fm.beginTransaction();
                //移除旧的fragment
                ft.remove(f);
                //换成新的fragment
                f = (ShoppingGuidePageFragment) getItem(position);
                //添加新fragment时必须用前面获得的tag
                ft.add(container.getId(), f, fragmentTag);
                ft.attach(f);
                ft.commitAllowingStateLoss();
            }
            return f;
        }
    }*/

}
